import {usercount,userlist} from '../../../request/api'
const state = {
    userlist: [],
    pageSize: 2,
    pageNum: 1,
    count: 0
}
const getters = {
    getUser(state) {
        return state.userlist
    },
    getSize(state) {
        return state.pageSize
    },
    getCount(state) {
        return state.count
    }
}
const mutations = {
    REQ_USERLIST(state, payload) {
        state.userlist = payload
    },
    REQ_COUNT(state, payload) {
        state.count = payload
    },
    //封装一个修改页码的事件
    REQ_PAGENUM(state, payload) {
        state.pageNum = payload
    }
}
const actions = {
    getUserAction(context) {
        console.log(context);
        userlist({
                size: context.state.pageSize,
                page: context.state.pageNum
            })
            .then(res => {
                console.log(res, '管理员列表');
                let list = res.list ? res.list : []
                if (context.state.pageNum != 1 && list.length == 0) {
                    //重新触发页码修改
                    context.dispatch('changePageAction', context.state.pageNum - 1)
                }

                context.commit('REQ_USERLIST', res.list)
            })
    },
    getCountAction({commit}) {
        usercount()
            .then(res => {
                console.log(res, '条数');
                commit('REQ_COUNT', res.list[0].total)
            })
    },
    //封装一个切换页码并重新触发列表的事件
    changePageAction(context, payload) {
        console.log(payload, '组件传递的内容')
        //修改一下页码
        context.commit('REQ_PAGENUM', payload)
        //重新触发行动(接口列表)
        context.dispatch('getUserAction')
    },
}


export default {
    state,
    getters,
    mutations,
    actions,
    namespaced: true
}